const express = require('express');
const router = express.Router();
let db = require("../db.js");
// const multer = require('multer');
// const utility = require('utility')


//获取
router.get("/list", function (req, res) {
    db("select * from todo", null, function (err, arr) {
        if (err != null) {
            res.send({
                status: 1,
                message: "获取计划列表失败！",
            });
        } else {
            res.send({
                status: 0,
                message: "获取计划列表成功！",
                arr: arr
            });
        }
    });
});
//新增
router.post("/add", function (req, res) {
    db("insert into todo set ?", req.body, function (err, arr) {
        console.log(err);
        if (err != null) {
            res.send({
                status: 1,
                message: "新增计划失败！",
            });
        } else {
            res.send({
                status: 0,
                message: "新增计划成功！"
            });
        }
    });
});
//修改
router.post("/edit", function (req, res) {
    // 2.1  接受post数据  req.body 得到是对象
    // req.body {Id,name,alias}

    // 2.2  修改
    db("update todo set ? where id=?", [req.body, req.body.id], function (err, obj) {
        if (err != null) {
            res.send({
                status: 1,
                message: "修改计划失败！",
            });
        } else {
            res.send({
                status: 0,
                message: "修改计划成功！"
            });
        }
    });
});
//删除
router.get("/del/:id", function (req, res) {
    let Id = req.params.id;

    db("delete from todo where id=?", Id, function (err, obj) {
        // console.log(err, obj.affectedRows);
        if (err != null) {
            res.send({
                status: 1,
                message: "删除文章分类失败！",
            });
        } else {
            res.send({
                status: 0,
                message: "删除文章分类成功！"
            });
        }
    });
});
module.exports = router;